package org.hilo.boot.core.value;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

/**
 * the chain implement
 * 
 * @author zollty
 * @since 2018-03-20
 */
public class FieldInitValueChain implements FieldInitValue {
    
    private List<FieldInitValue> fieldInitList = new LinkedList<>();
    
    /**
     * Add the given object with highest precedence.
     */
    public void addFirst(FieldInitValue fieldInit) {
        this.fieldInitList.add(0, fieldInit);
    }

    /**
     * Add the given object with lowest precedence.
     */
    public void addLast(FieldInitValue fieldInit) {
        this.fieldInitList.add(fieldInit);
    }
    
    public void reset() {
        fieldInitList.clear();
    }

    @Override
    public boolean initField(Field field, Object obj, int type) {
        boolean ret = false;
        for (FieldInitValue fieldInit : fieldInitList) {
            ret = fieldInit.initField(field, obj, type);
            if (ret) {
                break;
            }
        }
        return ret;
    }

}
